package com.example.jwt.demo.utils;


import com.auth0.jwt.JWT;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public class TokenUtil {
    public static String getToken(){
        String token=getRequest().getHeader("token");
        System.out.println(token);

        if(token!=null){
            String userId= JWT.decode(token).getAudience().get(0);
            return userId;
        }else {
            return "没有token";
        }

    }

    private static HttpServletRequest getRequest() {
        ServletRequestAttributes requestAttributes= (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        return requestAttributes==null?null:requestAttributes.getRequest();
    }
}
